// import parseTime, formatTime and set to filter
export { parseTime, formatTime } from "@/utils";

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label;
  }
  return time + label + "s";
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time);
  if (between < 3600) {
    return pluralize(~~(between / 60), " minute");
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), " hour");
  } else {
    return pluralize(~~(between / 86400), " day");
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1e18, symbol: "E" },
    { value: 1e15, symbol: "P" },
    { value: 1e12, symbol: "T" },
    { value: 1e9, symbol: "G" },
    { value: 1e6, symbol: "M" },
    { value: 1e3, symbol: "k" },
  ];
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (
        (num / si[i].value)
          .toFixed(digits)
          .replace(/\.0+$|(\.[0-9]*[1-9])0+$/, "$1") + si[i].symbol
      );
    }
  }
  return num.toString();
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0)
    .toString()
    .replace(/^-?\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ","));
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
 *precision指代精确到小数点哪一位，可选0，1，2…
 *symbol则是数字前的符号，一般配置货币符号
 *decimal选项是配置小数点那一位的符号（替换小数点）
 *separator指代分隔符，按照三位数一个分隔符来格式化
 *scale 缩放倍数
 */
export function filterPriceFormat(str) {
  return Number(str || 0).tranFormat(2, -2, true);
}

// 师傅服务类目接单状态
export function filterWorkerCategoryState(str) {
  let label = "";
  switch (str) {
    case "YES":
      label = "接单";
      break;
    case "NO":
      label = "关闭";
      break;
    default:
      label = `未知类型：${str}`;
      break;
  }
  return label;
}
// 零工平台签约状态
export function filterContractedStatus(str) {
  let label = "";
  switch (str) {
    case "Contracted":
      label = "已签约";
      break;
    case "NotContracted":
      label = "未签约";
      break;
    default:
      label = `未知类型：${str}`;
      break;
  }
  return label;
}
// 师傅接单状态
export function filterUserState(str) {
  let label = "";
  switch (str) {
    case "YES":
      label = "接单";
      break;
    case "NO":
      label = "关闭";
      break;
    default:
      label = `未知类型：${str}`;
      break;
  }
  return label;
}
// 服务区域重量级
export function filterAreaWeight(str) {
  let label = "";
  if (str == "IMPORTANT_POINT") {
    label = "重";
  } else if (str == "PRECEDENCE") {
    label = "优";
  } else {
    label = `普`;
  }
  return label;
}

// 入驻申请审核状态
export function filterAuditResultCode(str) {
  let label = "";
  if (str == "WAIT_AUDIT") {
    label = "待入驻";
  } else if (str == "AUDITING") {
    label = "待审核";
  } else if (str == "PASS") {
    label = "审核成功";
  } else if (str == "REFUSE") {
    label = "审核拒绝";
  } else {
    label = `--`;
  }
  return label;
}
// 订单状态
export function filterOrderStatus(str) {
  let label = "";
  if (str == "WAIT_PAY") {
    label = "已下单待支付";
  } else if (str == "WART_SEND") {
    label = "已支付待派单";
  } else if (str == "WART_APPOINTMENT") {
    label = "已派单待预约";
  } else if (str == "WART_SITE") {
    label = "已预约待上门";
  } else if (str == "WORKING") {
    label = "已上门施工中";
  } else if (str == "WART_CHECK") {
    label = "已完工待验收";
  } else if (str == "FINISH") {
    label = "验收完成，订单完结";
  } else if (str == "REFUND_CLOSE") {
    label = "订单退款关闭";
  } else if (str == "CANCEL") {
    label = "订单取消";
  } else {
    label = `全部`;
  }
  return label;
}
// 到货情况
export function filterArrivalEnum(str) {
  if (str == "REACH") {
    return "货物已到客户家";
  } else if (str == "NOT_REACH") {
    return "货物未到客户家";
  }
  return "--";
}
// 服务类型
export function filterServiceCategoryTypeEnum(str) {
  if (str == "INSTALLATION") {
    return "安装";
  } else if (str == "REPAIR") {
    return "维修";
  } else if (str == "MEASUREMENT") {
    return "测量";
  }
  return "--";
}

// 变更操作类型
export function filterChangeTypeEnum(str) {
  if (str == "SEND_ORDER") {
    return "派单";
  } else if (str == "SYNCHRONOUS") {
    return "同步服务商";
  } else if (str == "APPOINTMENT") {
    return "师傅预约上门时间";
  } else if (str == "UPDATE_APPOINTMENT") {
    return "师傅修改预约上门时间";
  } else if (str == "START_WORK") {
    return "师傅上门签到，上传检测照，开始施工";
  } else if (str == "FINISH_WORK") {
    return "师傅施工完成，上传完工照";
  } else if (str == "CANCEL_ORDER") {
    return "取消订单";
  } else if (str == "APPLY_REFUND") {
    return "发起售后";
  } else if (str == "FINISH_ORDER") {
    return "验收完成";
  } else if (str == "EVALUATE_ORDER") {
    return "服务评价";
  } else if (str == "REFUND_CLOSE") {
    return "订单退款关闭";
  }
}

// 是否有实体店
export function filterStoresFlag(str) {
  if (str == "YES") {
    return "是";
  } else if (str == "NO") {
    return "否";
  }
}
// 是否销售徒弟订单分成
export function filterEnjoySharing(str) {
  if (str == "YES") {
    return "是";
  } else if (str == "NO") {
    return "否";
  }
}
// 商家开启状态枚举
export function filterMerchantStatusEnum(str) {
  if (str == "YES") {
    return "已开启";
  } else if (str == "NO") {
    return "已停用";
  }
}
// 流水来源类型枚举
export function filterWaterComeInType(str) {
  if (str == "CONSUMPTION") {
    return "订单消费";
  } else if (str == "REFUND") {
    return "订单退款";
  } else if (str == "INDEMNITY") {
    return "售后赔款";
  } else if (str == "ORDER_COMMISSION") {
    return "订单佣金";
  } else if (str == "APPRENTICE_COMMISSION") {
    return "徒弟佣金";
  } else if (str == "AFTER_SALES_DEDUCTION") {
    return "售后扣款";
  } else if (str == "FAVORABLE_REWARD") {
    return "好评奖励";
  } else if (str == "订单取消") {
    return "好评奖励";
  } else if (str == "EXTRA_SERVICE_CONSUMPTION") {
    return "加价单消费";
  } else if (str == "CANCEL") {
    return "订单取消";
  }
  return "--";
}

// 流水结算类型枚举
export function filterSettleState(str) {
  if (str == "UNSETTLED") {
    return "待结算";
  } else if (str == "SETTLED") {
    return "已结算";
  } else {
    return "--";
  }
}
// 流水类型枚举
export function filterSettleType(str) {
  if (str == "INCREASE") {
    return "正向结算";
  } else if (str == "DECREASE") {
    return "逆向结算";
  } else {
    return "--";
  }
}
// 充值类型枚举
export function filterRechargeChangeType(str) {
  if (str == "MEMBER_BALANCE_REFUND_BALANCE") {
    return "退款";
  } else if (str == "MEMBER_BALANCE_RECHARGE") {
    return "充值";
  } else if (str == "WORKER_SECURITY_DEPOSIT_REFUND") {
    return "保证金退款";
  } else {
    return "--";
  }
}

// 充值审核枚举
export function filterRechargeChangeState(str) {
  if (str == "APPROVAL_ING") {
    return "审批中";
  } else if (str == "APPROVAL_REFUSE") {
    return "审批拒绝";
  } else if (str == "APPROVAL_FINISH") {
    return "已完成";
  } else if (str == "APPROVAL_REVOKE") {
    return "已撤回";
  } else {
    return "--";
  }
}

// 售后状态枚举
export function filterSaleStatusEnum(str) {
  if (str == "WAITING") {
    return "待处理";
  } else if (str == "CANCEL") {
    return "已取消";
  } else if (str == "REFUSE") {
    return "已拒绝";
  } else if (str == "FINISH") {
    return "已完成";
  } else {
    return "--";
  }
}

// 售后单来源
export function filterSaleSourceEnum(str) {
  if (str == "MERCHANT") {
    return "商家提交";
  } else if (str == "MANUAL") {
    return "手动创建";
  } else {
    return "--";
  }
}
// 保证金退款审核枚举
export function filterBailApprovalState(str) {
  if (str == "COMPLETED") {
    return "已完成";
  } else if (str == "PENDING_PAYMENT") {
    return "待付款";
  } else if (str == "APPROVAL_PENDING") {
    return "审批中";
  } else if (str == "APPROVAL_REJECTED") {
    return "审批拒绝";
  } else {
    return "--";
  }
}

// poi来源枚举
export function filterOnboardingSource(str) {
  if (str == "APPLY_IN_PERSON") {
    return "师傅申请";
  } else if (str == "Operation_CREATE") {
    return "运营创建";
  } else {
    return "--";
  }
}
// 师傅流水状态
export function filterRunWaterChangeState(str) {
  if (str == "WITHDRAWING") {
    return "提现中";
  } else if (str == "SUCCESS") {
    return "提现成功";
  } else if (str == "FAIL") {
    return "提现失败";
  }
}
// 师傅流水类型
export function filterRunWaterChangeType(str) {
  if (str == "CONSUMPTION") {
    return "订单消费";
  } else if (str == "REFUND") {
    return "订单退款";
  } else if (str == "INDEMNITY") {
    return "售后赔款";
  } else if (str == "ORDER_COMMISSION") {
    return "订单佣金";
  } else if (str == "APPRENTICE_COMMISSION") {
    return "徒弟佣金";
  } else if (str == "AFTER_SALES_DEDUCTION") {
    return "售后扣款";
  } else if (str == "FAVORABLE_REWARD") {
    return "好评奖励";
  } else if (str == "WITHDRAWAL") {
    return "提现";
  } else if (str == "EXTRA_SERVICE_CONSUMPTION") {
    return "加价单消费";
  } else if (str == "CANCEL") {
    return "取消订单";
  }
}
// 支付方式
export function filterPayMethod(str) {
  if (str == "BALANCE") {
    return "余额支付";
  } else if (str == "ALIPAY") {
    return "支付宝支付";
  } else if (str == "WECHAT") {
    return "微信支付";
  }
}

export function filterBailChangeType(str) {
  if (str == "RECHARGE") {
    return "充值余额";
  } else if (str == "CONSUMPTION") {
    return "订单消费";
  } else if (str == "REFUND") {
    return "订单退款";
  } else if (str == "INDEMNITY") {
    return "售后赔款";
  } else if (str == "ORDER_COMMISSION") {
    return "订单佣金";
  } else if (str == "APPRENTICE_COMMISSION") {
    return "徒弟佣金";
  } else if (str == "AFTER_SALES_DEDUCTION") {
    return "售后扣款";
  } else if (str == "FAVORABLE_REWARD") {
    return "好评奖励";
  } else if (str == "WITHDRAWAL") {
    return "提现";
  } else if (str == "WITHDRAWAL_RETURN") {
    return "提现退回";
  } else if (str == "WORKER_SECURITY_DEPOSIT_RECHARGE") {
    return "保证金充值";
  } else if (str == "WORKER_SECURITY_DEPOSIT_REFUND") {
    return "保证金退款";
  } else if (str == "WORKER_SECURITY_DEPOSIT_REFUND_RETURN") {
    return "保证金退款退回";
  } else if (str == "MEMBER_BALANCE_RECHARGE") {
    return "余额充值";
  } else if (str == "MEMBER_BALANCE_REFUND_BALANCE") {
    return "余额退款";
  } else if (str == "MEMBER_BALANCE_REFUND_BALANCE_RETURN") {
    return "余额退款退回";
  }
}
